package com.heihx.mall.product.service.impl;

import com.heihx.common.constant.ProductConstant;
import com.heihx.mall.product.dao.AttrAttrgroupRelationDao;
import com.heihx.mall.product.dao.AttrGroupDao;
import com.heihx.mall.product.dao.CategoryDao;
import com.heihx.mall.product.entity.AttrAttrgroupRelationEntity;
import com.heihx.mall.product.entity.AttrGroupEntity;
import com.heihx.mall.product.entity.CategoryEntity;
import com.heihx.mall.product.entity.vo.AttrRespVO;
import com.heihx.mall.product.entity.vo.AttrVO;
import com.heihx.mall.product.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heihx.common.utils.PageUtils;
import com.heihx.common.utils.Query;

import com.heihx.mall.product.dao.AttrDao;
import com.heihx.mall.product.entity.AttrEntity;
import com.heihx.mall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveAttr(AttrVO attr) {
        //创建一个dao实体类
        AttrEntity attrEntity = new AttrEntity();
//      attrEntity.setAttrName(attr.getAttrName());
        //把传来的基本数据传给dao实体类
        BeanUtils.copyProperties(attr,attrEntity);
        //1、保存基本数据
        this.save(attrEntity);
        //2、保存关联关系
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(attr.getAttrGroupId());
        relationEntity.setAttrId(attrEntity.getAttrId());
        relationDao.insert(relationEntity);

    }

    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId, String type) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type","base".equalsIgnoreCase(type)? ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode():ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());

        //1 创建一个新的queryWrapper AttrEntity
//        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        //2 如果传入的catelogId不为0 我们才查询catelogId
        if(catelogId != 0){
            queryWrapper.eq("catelog_id",catelogId);
        }

        //3 得到params参数列表中的Key 如果不为空 进行规则匹配(返回属性id等于Key或者是属性name like key)
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            //attr_id  attr_name
            queryWrapper.and((wrapper)->{
                wrapper.eq("attr_id",key).or().like("attr_name",key);
            });
        }

        //4 调用mybatis-plus serviceImpl层的page方法返回Ipage对象
        // 其参数是根据Query工具类生成的Page对象 和 queryWrapper
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );

        //5 把生成的Ipage对象封装到pageUtils里
        PageUtils pageUtils = new PageUtils(page);

        //6 从Ipage得到记录
        List<AttrEntity> records = page.getRecords();
        List<AttrRespVO> respVos = records.stream().map((attrEntity) -> {
            // 生成一个AttrRespVo的VO对象
            AttrRespVO attrRespVo = new AttrRespVO();
            // 先把原来的数据copy到其中
            BeanUtils.copyProperties(attrEntity, attrRespVo);

            //1、设置分类和分组的名字
            if("base".equalsIgnoreCase(type)){
                AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (attrId != null && attrId.getAttrGroupId()!=null) {
                    AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId());
                    attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }

            }


            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrRespVo.setCatelogName(categoryEntity.getName());
            }
            return attrRespVo;
        }).collect(Collectors.toList());

        pageUtils.setList(respVos);
        return pageUtils;
    }

    @Override
    public AttrRespVO getAttrInfo(Long attrId) {
        //1 新建一个respVo
        AttrRespVO respVo = new AttrRespVO();
        //2 根据传入id获取当前属性的实体类
        AttrEntity attrEntity = this.getById(attrId);
        //3 先把部分数据封装到respVO当中
        BeanUtils.copyProperties(attrEntity,respVo);

        //4 如果是基本属性
        if(attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
            //1、设置分组信息
            AttrAttrgroupRelationEntity attrgroupRelation = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
            if(attrgroupRelation!=null){
                respVo.setAttrGroupId(attrgroupRelation.getAttrGroupId());
                AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupRelation.getAttrGroupId());
                if(attrGroupEntity!=null){
                    respVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }
        }


        //2、设置分类信息
        Long catelogId = attrEntity.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        respVo.setCatelogPath(catelogPath);

        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if(categoryEntity!=null){
            respVo.setCatelogName(categoryEntity.getName());
        }


        return respVo;
    }


}